#include <stdio.h>
#include <unistd.h>//是close， write这些接口的头文件
#include <string.h>
#include <fcntl.h>//是 O_CREAT 这些宏的头文件
#include <sys/stat.h>//umask接口头文件

int main()
{
    //将当前进程的默认文件创建权限掩码设置为0--- 并不影响系统的掩码，仅在当前进程内生效
    umask(0);
    //int open(const char *pathname, int flags, mode_t mode);
    int fd = open("./tmp.txt", O_CREAT|O_RDWR|O_APPEND, 0777);
    if(fd < 0) {
        perror("open error");
        return -1;
    }

    char *data = "雷猴啊~老铁~\n";
    //ssize_t write(int fd, const void *buf, size_t count);
    ssize_t ret = write(fd, data, strlen(data));
    if (ret < 0) {
        perror("write error");
        return -1;
    }

    //off_t lseek(int fd, off_t offset, int whence);
    lseek(fd, 0, SEEK_SET);

    char buf[1024] = {0};
    //ssize_t read(int fd, void *buf, size_t count);
    ret = read(fd, buf, 1023);
    if (ret < 0) {
        perror("read error");
        return -1;
    }else if (ret == 0) {
        printf("end of file!\n");
        return -1;
    }
    printf("%d-%s", ret, buf);


    close(fd);
    return 0;
}
